//
//  BackupsSwitchView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/27.
//

import SwiftUI
import Kingfisher

//历史评论弹窗试图
struct HistoryCommentDialogView: View {
    @EnvironmentObject var themeManager: ThemeManager
    @StateObject var viewModel = HistoryCommentViewModel()
    @State private var showChangeAi = false //更换回应
    @State private var content: String = ""
    @State private var select: Int = -1
    @Binding var bean : DiaryBean
    var aiRoleId: String
    let onDismiss: () -> Void

    var body: some View {
        let aiChat2Text = getText()
        let list = getList()
        PopupBottomView(title: "历史回应") {
            VStack{
                if (list.isEmpty) {
                    HomeEmptyView(text1: "暂无回应")
                        .frame(maxHeight: .infinity, alignment: .center)
                } else {
                    ScrollView {
                        //需要嵌套一层LazyVStack，不然会产生间距，而且大小还会随机
                        LazyVStack(spacing: 10) {
                            ForEach(Array(list.enumerated()), id: \.offset) { index, item in
                                HStack {
                                    Text("\(index + 1)、\(item)")
                                        .font(.system(size: 16))
                                        .foregroundStyle(textColor(for: item, comparedTo: aiChat2Text))
                                        .padding(.top, 15)
                                        .contentShape(Rectangle())
                                        .onTapGesture{
                                            select = index
                                            showChangeAi.toggle()
                                        }
                                }.frame(maxWidth:.infinity, alignment: .topLeading)
                                
                            }
                        }
                    }
                    .frame(maxHeight: .infinity, alignment: .top)
                    .padding(.top, 20)
                }
            }.frame(maxHeight: .infinity, alignment: .center)
                .padding(.bottom, 10)
        }.alert("温馨提示",isPresented:$showChangeAi) {
            Button("取消",role: .cancel){}
            Button("确定",role: .destructive) {
                if select >= 0 {
                    viewModel.change(bean: $bean,aiRoleId: aiRoleId,text: list[select])
                }
            }
        } message: {
            Text("是否确定更换该回应")
        }
        .onReceive(viewModel.$changeSuc) { value in
            if value {
                onDismiss()
            }
        }
    }
    
    private func getText() ->String {
        return bean.getAiReply(aiRoleId: aiRoleId)
    }
    
    private func getList() -> [String] {
        var list : [String] = []
        if bean.isMultiple() {
            list = bean.aiRule?.listAiRole?.first { $0.roleId == aiRoleId }?.content?.map { $0 } ?? []
        } else {
            list = bean.aiChat2?.listChat?.map { $0.text ?? "" } ?? []
        }
        return list
    }
    
    private func textColor(for text: String, comparedTo otherText: String) -> Color {
        return text == otherText ? themeManager.themeColor : .bgNormalText
    }
}


//#Preview {
//    DiaryCommentDialogView()
//}

//定义预览提供者
struct HistoryCommentDialogView_Previews: PreviewProvider {
    static var previews: some View {
        @State var showHistoryComment = false //历史评论
        @State var bean : DiaryBean = DiaryBean(content: "日记内容")

        //使用预览共享配置
        configurePreviewEnvironment(for: HistoryCommentDialogView(bean: $bean,aiRoleId: ""){})
    }
}
